﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;


/// <summary>
/// 快速Tween效果
/// </summary>
public class PresetTween : MonoBehaviour
{

    public enum TweenType
    {
        PunchScale,
        PunchRotate,
        FadeInOut,
        FadeWave,
        JellyBounce
    }

    public float duration = 0.3f;

    public float power = 1.0f;

    private bool isTween = false;

    public TweenType type = TweenType.PunchScale;

    private Vector3 _scale = Vector3.one;

    private void Start()
    {
        _scale = transform.localScale;
    }


    public void RunTween()
    {
        if (isTween) return;
        Tween tween;
        isTween = true;

        switch (type)
        {
            case TweenType.PunchScale:
                transform.localScale = _scale;
                tween = transform.DOPunchScale(new Vector3(0.1f, 0.1f, 0.1f) * power, duration);
                break;
            case TweenType.PunchRotate:
                tween = transform.DOPunchRotation(new Vector3(0, 0, 30f) * power, duration);
                break;
            case TweenType.FadeInOut:
                var sequence = DOTween.Sequence();
                var graphic = transform.GetComponent<Graphic>();
                sequence.Append(graphic.DOFade(1 * Mathf.Clamp(1 - power, 0, 1), 0.5f * duration));
                sequence.Append(graphic.DOFade(1.0f, 0.5f * duration));
                tween = sequence;
                break;
            case TweenType.FadeWave:
                sequence = DOTween.Sequence();
                transform.localScale = Vector3.zero;
                graphic = transform.GetComponent<Graphic>();
                sequence.Append(graphic.DOFade(1, 0.5f * duration));
                sequence.Append(graphic.DOFade(0f, 0.5f * duration));
                sequence.Insert(0, transform.DOScale(Vector3.one * (1 + power), duration));
                tween = sequence;
                break;
            case TweenType.JellyBounce:
                sequence = DOTween.Sequence()
                .Append(transform.DOScale(new Vector3(1f + 0.1f * power, 1 - 0.1f * power, 1f), 0.33f * duration))
                .Append(transform.DOScale(new Vector3(1 - 0.1f * power, 1 + 0.05f * power, 1f), 0.33f * duration))
                .Append(transform.DOScale(new Vector3(1f, 1f, 1f), 0.33f * duration))
                ;
                tween = sequence;
                break;
            default:
                transform.localScale = _scale;
                tween = transform.DOPunchScale(new Vector3(0.1f, 0.1f, 0.1f) * power, duration);
                break;
        }

        tween.SetId(gameObject);

        tween.onComplete += () =>
        {
            isTween = false;
        };
    }

    private void OnDisable()
    {
        DOTween.Kill(gameObject);
    }


}
